package com.example.dwr.reverseajax;

import java.util.LinkedList;
import org.directwebremoting.Browser;
import org.directwebremoting.ui.dwr.Util;

public class JavaChat
{
  private final LinkedList<Message> messages;

  public JavaChat()
  {
    this.messages = new LinkedList();
  }

  public void addMessage(String text)
  {
    if ((text != null) && (text.trim().length() > 0))
    {
      this.messages.addFirst(new Message(text));
      while (this.messages.size() > 10)
      {
        this.messages.removeLast();
      }

    }

    Util.setValue("text", "");

    Browser.withCurrentPage(new Runnable()
    {
      public void run()
      {
        Util.removeAllOptions("chatlog");
        Util.addOptions("chatlog", messages, "text");
      }
    });
  }
}